package cn.gson.crm.controller.money;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gson.crm.model.dao.HightSchoolTuitionDao;
import cn.gson.crm.model.dao.PayrecordDao;
import cn.gson.crm.model.dao.UniversityTuitionDao;
import cn.gson.crm.model.entity.HightSchoolTuition;
import cn.gson.crm.model.entity.Payrecord;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.entity.UniversityTuition;
import cn.gson.crm.model.mapper.HeightSchoolTuitionMapper;
import cn.gson.crm.model.mapper.PayrecordMapper;
import lombok.extern.log4j.Log4j;

@Controller
@RequestMapping("/hight")
public class HightSchoolTuitionController {

	@Autowired
	private HightSchoolTuitionDao hightDao;
	
	@Autowired
	private PayrecordMapper payRecordMapper;
	
	@Autowired
	private UniversityTuitionDao uniDao; 
	
	@Autowired
	private HeightSchoolTuitionMapper hightMapper;
	
	/**
	 * 查询高中生的学费信息
	 * @param payrecord
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(HightSchoolTuition hight, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<HightSchoolTuition> pageData = hightMapper.findAllHeightTuition(hight);
		 
		PageInfo<HightSchoolTuition> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	/**
	 * 删除高中生学费信息
	 */
	
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		HightSchoolTuition p;
		if (id != null && (p = hightDao.findOne(id)) != null) {
			p.setDeleted(false);
			obj.put("success", true);
		} else {
			obj.put("message", "收款记录不存在！");
		}
		return obj;
	}
	
	/**
	 * 编辑表单
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping({ "edithight", "edithight/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			HightSchoolTuition p = hightDao.findOne(id);	
			if (p != null) {		
				model.addAttribute("hight", p);
			}
		}	
		List<Map<String,Object>> hight=hightMapper.selectAllStudentName();
		model.addAttribute("hights",hight);
		
		List<Map<String,Object>> className=hightMapper.selectAllClassName();
		model.addAttribute("className",className);
		return "hight/edithight";
	}

	/**
	 * 保存
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("saveHight")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(@Valid HightSchoolTuition hight, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			if (hight.getStudents().getId() == null) {
					obj.put("message", "该用户不存在！");
					return obj;
				
			} else {
				hight.setDeleted(true);
				hightDao.save(hight);
				obj.put("success", true);
			}	
		}
		return obj;
	}
	
	/**
	 * 将收款记录导出到学费信息表
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("daoChu/{names}")
	@ResponseBody
	public String daoChu(@PathVariable(name="names",required = false) Long[] stuId) {
		Map<String,Object> obj = new HashMap<String, Object>();
			//将根据id将收款记录读取出来
			List<Payrecord> diss = new ArrayList<Payrecord>();
			for(int i=0;i<stuId.length;i++) {
				Payrecord record=new Payrecord();
				record=payRecordMapper.findPayreordById(stuId[i]);
				diss.add(i,record);
			}
			//获取学员身份
			for(int i=0;i<diss.size();i++) {
				String identity=diss.get(i).getIdentity();
				if(("高中生").equals(identity)){
					List<HightSchoolTuition> list = new ArrayList<HightSchoolTuition>();
					HightSchoolTuition hight=new HightSchoolTuition();
					hight.setStudents(diss.get(i).getStudent());
					hight.setYingJiao(diss.get(i).getAllMoney());
					hight.setShiJiao(0d);
					hight.setClasses(diss.get(i).getClasses());
					hight.setDeleted(true);
					System.err.println(diss.get(i).getQianJiao());
					hight.setQianJiao(diss.get(i).getQianJiao());
					hightDao.save(hight);
				}else if(("大学生").equals(identity)) {
					List<UniversityTuition> list = new ArrayList<UniversityTuition>();
					UniversityTuition uni=new UniversityTuition();
					uni.setStudent(diss.get(i).getStudent());
					uni.setYingJiao(diss.get(i).getAllMoney());
					uni.setShiJiao(321d);
					uni.setClasses(diss.get(i).getClasses());
					uni.setFenQi("未签");
					uni.setDeleted(true);
					uni.setQianJiao(diss.get(i).getQianJiao());
					uniDao.save(uni);
				}else {
					//身份为空
				}
			}
			
			return "hight/list";
	}
	/**
	 * 导入
	 */
	@RequestMapping("daoru")
	public String daoru(){	
		return "hight/daoru";
	}
}
